home *** CD-ROM | disk | FTP | other *** search
/ SGI Hot Mix 17 / Hot Mix 17.iso / HM17_SGI / research / lib / search2d.pro < prev    next >
Text File  |  1997-07-08  |  6KB  |  154 lines

  1. ; $Id: search2d.pro,v 1.5 1997/01/15 03:11:50 ali Exp $
  2.  
  3. ; Copyright (c) 1992-1997, Research Systems, Inc. All rights reserved.
  4. ;    Unauthorized reproduction prohibited.
  5. ;
  6. ;+
  7. ; NAME:
  8. ;    SEARCH2D
  9. ;
  10. ; PURPOSE:
  11. ;    This function finds "objects" or regions of similar data
  12. ;       values within a 2-D array of data. Given a starting location
  13. ;       and a range of values to search for, SEARCH2D will find all
  14. ;       the cells within the array that are within the specified range
  15. ;       of values, and have some path of connectivity through these cells
  16. ;       to the starting location. See the procedure "SEARCH3D" for the
  17. ;       three dimensional case.
  18. ;
  19. ;       This function returns a list of the array subscripts that define
  20. ;       the selected object or region.
  21. ;
  22. ; CATEGORY:
  23. ;       Data subsetting.
  24. ;    Image manipulation.
  25. ;
  26. ; CALLING SEQUENCE:
  27. ;       Region = SEARCH2D(Array, Xpos, Ypos, Min_val, Max_val)
  28. ;
  29. ; INPUTS:
  30. ;       Array:      The 2-D array of data to search.
  31. ;                   Data type : Any 2-D array except string or structure.
  32. ;       Xpos:       The X coordinate(s) (first subscript into the 2-D Array)
  33. ;                   of the seed point(s) for the search.   Xpos can be a
  34. ;                   single value, or an array of subscripts specifying the
  35. ;                   X coordinates of a seed region.   Xpos must have the same
  36. ;                   number of elements as Ypos.
  37. ;                   Data type : Long or Lonarr.
  38. ;       Ypos:       The Y coordinate(s) (second subscript into the 2-D Array)
  39. ;                   of the seed point(s) for the search.   Ypos can be a
  40. ;                   single value, or an array of subscripts specifying the
  41. ;                   Y coordinates of a seed region.   Ypos must have the same
  42. ;                   number of elements as Xpos.
  43. ;                   Data type : Long or Lonarr.
  44. ;       Min_val:    The minimum data value to search for. All cells that
  45. ;                   are connected to the starting cell, and have a value
  46. ;                   greater than or equal to Min_val and less than or equal
  47. ;                   to Max_val, will be considered part of the "object".
  48. ;                   If omitted, the default is MIN(Array[Xpos, Ypos]) .
  49. ;       Max_val:    The maximum data value to search for.
  50. ;                   If omitted, the default is MAX(Array[Xpos, Ypos]) .
  51. ;
  52. ; KEYWORD PARAMETERS:
  53. ;       IMAGE:      If set, SEARCH2D returns a bi-level image (2-D byte array)
  54. ;                   with the same dimensions as Array, containing the value
  55. ;                   (1) where the object is, and (0) where it isn't.
  56. ;
  57. ;       DIAGONAL:   This keyword is now obsolete since LABEL_REGION is now
  58. ;                   used to find the object.
  59. ;       DECREASE:   This keyword is now obsolete as are INCREASE and LPF_BAND.
  60. ;                   A better way to perform this operation is to pre-process
  61. ;                   the Array parameter before passing to SEARCH2D.
  62. ;       INCREASE:   See the DECREASE keyword.
  63. ;       LPF_BAND:   See the DECREASE keyword.
  64. ;
  65. ; OUTPUTS:
  66. ;       This function returns a list of the indices into the 2-D array
  67. ;       that are part of the located object or region. This list is
  68. ;       returned as a LONARR(n) where n is the number of cells found.
  69. ;
  70. ;       If the returned array of indices is called Region, and the
  71. ;       size of the 2-D array of data is size_x by size_y, then the
  72. ;       actual X and Y indices can be obtained by using the following
  73. ;       algorithm :
  74. ;
  75. ;          index_y = Region / size_x
  76. ;          index_x = Region - (index_y * size_x)
  77. ;
  78. ;       The object within the 2-D Array could then be subscripted as :
  79. ;
  80. ;          Array[Region]
  81. ;       OR
  82. ;          Array[index_x, index_y]
  83. ;
  84. ;       If the IMAGE keyword is set, however, SEARCH2D returns a bi-level
  85. ;       image (2-D byte array) with the same dimensions as Array, containing
  86. ;       the value (1) where the object is, and (0) where it isn't.
  87. ;
  88. ; EXAMPLE:
  89. ;       Find all the indices corresponding to an object contained in a
  90. ;       2-D array of data.
  91. ;
  92. ;       ; Create some data.
  93. ;          img = FLTARR(512, 512)
  94. ;          img[3:503, 9:488] = 0.7
  95. ;          img[37:455, 18:438] = 0.5
  96. ;          img[144:388, 90:400] = 0.7
  97. ;          img[200:301, 1:255] = 1.0
  98. ;          img[155:193, 333:387] = 0.3
  99. ;
  100. ;       ; Display the image.
  101. ;          TVSCL, img
  102. ;
  103. ;       ; Search for an object starting at (175, 300) whose data values
  104. ;       ; are between (0.6) and (0.8).
  105. ;          object = SEARCH2D(img, 175, 300, 0.6, 0.8, /IMAGE)
  106. ;       ; Display the object.
  107. ;          TVSCL, object
  108. ;
  109. ; MODIFICATION HISTORY:
  110. ;       Written by:     Daniel Carr. Thu Sep  3 15:36:17 MDT 1992
  111. ;       Modified:       Daniel Carr.
  112. ;                       Re-wrote to improve performance using "LABEL_REGION".
  113. ;                       Obsoleted keywords INCREASE, DECREASE, and LPF_BAND.
  114. ;                       Added IMAGE keyword.
  115. ;-
  116.  
  117. FUNCTION Search2d, array, xpos, ypos, min_val, max_val, Diagonal=diagonal, $
  118.                    Image=image, Decrease=decrease, Increase=increase, $
  119.                    Lpf_band=smooth_band
  120.  
  121. ON_ERROR, 2
  122.  
  123. IF (Keyword_Set(diagonal)) THEN $
  124.    Print, 'Search2D: Obsolete keyword "DIAGONAL" ignored.'
  125. IF (N_Elements(decrease) GT 0L) THEN $
  126.    Print, 'Search2D: Obsolete keyword "DECREASE" ignored.'
  127. IF (N_Elements(increase) GT 0L) THEN $
  128.    Print, 'Search2D: Obsolete keyword "INCREASE" ignored.'
  129. IF (N_Elements(smooth_band) GT 0L) THEN $
  130.    Print, 'Search2D: Obsolete keyword "LPF_BAND" ignored.'
  131.  
  132. IF (N_Elements(min_val) LE 0L) THEN min_val = MIN(array[xpos, ypos], Max=max_val)
  133. IF (N_Elements(max_val) LE 0L) THEN max_val = MAX(array[xpos, ypos])
  134.  
  135. size_array = Size(array)
  136. s_array = Bytarr(size_array[1], size_array[2])
  137. index = Where((array GE min_val) AND (array LE max_val))
  138. IF (index[0] GE 0L) THEN s_array[Temporary(index)] = 1B
  139.  
  140. s_array[xpos, ypos] = 1B
  141.  
  142. s_array = Label_Region(Temporary(s_array))
  143. seed_point = s_array[xpos[0], ypos[0]]
  144. index = Where(Temporary(s_array) eq seed_point)
  145.  
  146. IF (Keyword_Set(image)) THEN BEGIN
  147.    s_array = Bytarr(size_array[1], size_array[2])
  148.    IF (index[0] GE 0L) THEN s_array[Temporary(index)] = 1B
  149.    RETURN, s_array
  150. ENDIF ELSE RETURN, index
  151.  
  152. END
  153.